package com.explorex.infra.puma.gateway.netty.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SpringContextUtils implements ApplicationContextAware {

    private static  ApplicationContext _applicationContext=null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this._applicationContext=applicationContext;
    }

    public static <T> T getBean(Class<T> requiredType) {
        if (_applicationContext != null) {
            return _applicationContext.getBean(requiredType);
        }
        return null;
    }

    public static <T> T getBean(String beanName,Class<T> requiredType) {
        if (_applicationContext != null) {
            return _applicationContext.getBean(beanName,requiredType);
        }
        return null;
    }

    public static <T> Map<String, T> getBeansOfType(@Nullable Class<T> tClass){
        if (_applicationContext != null) {
            return _applicationContext.getBeansOfType(tClass);
        }
        return null;
    }

}
